using UnityEngine;
using UnityEngine.UI;

namespace PKGames
{
	public abstract class HorizontalGridLayouter : IGridLayouter
	{
		public RectTransform cellAnchor;

		public GridLayoutGroup cellGrid;

		public RectTransform content;

		public RectTransform viewport;

		public CanvasScaler canvasScaler;

		public float numberOfCells;

		private int _maximumNumberOfCells;

		public virtual float MinAspectRatio => 0f;

		public virtual float MaxAspectRatio => 0f;

		public virtual float MinCellSizeProportion => 0f;

		public virtual float MaxCellSizeProportion => 0f;

		public virtual float MinCellHorizontalSpaceProportion => 0f;

		public virtual float MaxCellHorizontalSpaceProportion => 0f;

		public virtual float MinCellVerticalSpaceProportion => 0f;

		public virtual float MaxCellVerticalSpaceProportion => 0f;

		public virtual float MinTopMarginProportion => 0f;

		public virtual float MaxTopMarginProportion => 0f;

		public virtual float MinBottomMarginProportion => 0f;

		public virtual float MaxBottomMarginProportion => 0f;

		public virtual float MinHorizontalMarginProportion => 0f;

		public virtual float MaxHorizontalMarginProportion => 0f;

		public virtual void UpdateLayout()
		{
			float value = (float)Screen.width / (float)Screen.height;
			float num = Mathf.Clamp(value, MinAspectRatio, MaxAspectRatio);
			float aspectRatioPercentage = (num - MinAspectRatio) / (MaxAspectRatio - MinAspectRatio);
			float contentSizeWithoutPadding = ContentSizeWithoutPaddingForAspectRatio(aspectRatioPercentage);
			AdjustConstraintsForAspectRatio(aspectRatioPercentage, contentSizeWithoutPadding);
		}

		private float ContentSizeWithoutPaddingForAspectRatio(float aspectRatioPercentage)
		{
			float num = CellSizeForAspectRatio(aspectRatioPercentage);
			float num2 = CellHorizontalSpacingForAspectRatio(aspectRatioPercentage);
			if (LayoutIsHorizontal())
			{
				return (numberOfCells * num + (numberOfCells - 1f) * num2) / (float)cellGrid.constraintCount;
			}
			return numberOfCells * num + (numberOfCells - 1f) * num2;
		}

		private bool LayoutIsHorizontal()
		{
			return cellGrid.constraint == GridLayoutGroup.Constraint.FixedRowCount && cellGrid.startAxis == GridLayoutGroup.Axis.Vertical;
		}

		private float CellSizeForAspectRatio(float aspectRatioPercentage)
		{
			return viewport.rect.height * Mathf.Lerp(MinCellSizeProportion, MaxCellSizeProportion, aspectRatioPercentage);
		}

		private float CellHorizontalSpacingForAspectRatio(float aspectRatioPercentage)
		{
			return viewport.rect.height * Mathf.Lerp(MinCellHorizontalSpaceProportion, MaxCellHorizontalSpaceProportion, aspectRatioPercentage);
		}

		private void AdjustConstraintsForAspectRatio(float aspectRatioPercentage, float contentSizeWithoutPadding)
		{
			AdjustGridConstraintsForAspectRatio(aspectRatioPercentage);
			SetMaxCellOnViewport();
			float cellAnchorHorizontalMargin = HorizontalMarginForAspectRatio(aspectRatioPercentage, contentSizeWithoutPadding);
			AdjustContentRect(contentSizeWithoutPadding, cellAnchorHorizontalMargin);
			AdjustCellAnchorRectForContent(aspectRatioPercentage, contentSizeWithoutPadding, cellAnchorHorizontalMargin);
		}

		private float HorizontalMarginForAspectRatio(float aspectRatioPercentage, float contentSizeWithoutPadding)
		{
			if (IsNumberOfCellsSmall())
			{
				return (1f - contentSizeWithoutPadding / viewport.rect.width) / 2f * viewport.rect.width;
			}
			Rect rect = viewport.rect;
			Vector2 referenceResolution = canvasScaler.referenceResolution;
			float num = referenceResolution.x * rect.width / rect.height * referenceResolution.y / referenceResolution.x;
			return Mathf.Lerp(MinHorizontalMarginProportion, MaxHorizontalMarginProportion, aspectRatioPercentage) * num;
		}

		private void AdjustGridConstraintsForAspectRatio(float aspectRatioPercentage)
		{
			SetupCellSize(aspectRatioPercentage);
			SetupCellSpacing(aspectRatioPercentage);
			SetupVerticalMargins(aspectRatioPercentage);
		}

		private void SetupCellSize(float aspectRatioPercentage)
		{
			float num = CellSizeForAspectRatio(aspectRatioPercentage);
			cellGrid.cellSize = new Vector2(num, num);
		}

		private void SetupCellSpacing(float aspectRatioPercentage)
		{
			float x = CellHorizontalSpacingForAspectRatio(aspectRatioPercentage);
			float y = CellVerticalSpacingForAspectRatio(aspectRatioPercentage);
			cellGrid.spacing = new Vector2(x, y);
		}

		private float CellVerticalSpacingForAspectRatio(float aspectRatioPercentage)
		{
			return viewport.rect.height * Mathf.Lerp(MinCellVerticalSpaceProportion, MaxCellVerticalSpaceProportion, aspectRatioPercentage);
		}

		private void SetupVerticalMargins(float aspectRatioPercentage)
		{
			cellGrid.padding.top = (int)(viewport.rect.height * Mathf.Lerp(MinTopMarginProportion, MaxTopMarginProportion, aspectRatioPercentage));
			cellGrid.padding.bottom = (int)(viewport.rect.height * Mathf.Lerp(MinBottomMarginProportion, MaxBottomMarginProportion, aspectRatioPercentage));
		}

		private void AdjustContentRect(float contentSizeWithoutPadding, float cellAnchorHorizontalMargin)
		{
			content.sizeDelta = new Vector2(2f * cellAnchorHorizontalMargin + contentSizeWithoutPadding, viewport.rect.height);
		}

		private void AdjustCellAnchorRectForContent(float aspectRatioPercentage, float contentSizeWithoutPadding, float cellAnchorHorizontalMargin)
		{
			float num = cellAnchorHorizontalMargin / (2f * cellAnchorHorizontalMargin + contentSizeWithoutPadding);
			RectTransform rectTransform = cellAnchor;
			float x = num;
			Vector2 anchorMin = cellAnchor.anchorMin;
			rectTransform.anchorMin = new Vector2(x, anchorMin.y);
			RectTransform rectTransform2 = cellAnchor;
			float x2 = 1f - num;
			Vector2 anchorMax = cellAnchor.anchorMax;
			rectTransform2.anchorMax = new Vector2(x2, anchorMax.y);
		}

		private void SetMaxCellOnViewport()
		{
			Vector2 cellSize = cellGrid.cellSize;
			float x = cellSize.x;
			Vector2 spacing = cellGrid.spacing;
			float num = x + spacing.x;
			_maximumNumberOfCells = Mathf.FloorToInt(viewport.rect.width / num) * cellGrid.constraintCount;
		}

		private bool IsNumberOfCellsSmall()
		{
			if (numberOfCells <= (float)_maximumNumberOfCells)
			{
				return true;
			}
			return false;
		}
	}
}
